//
//@File    :	12.8_placenew1.cpp
//@Time    :	2021/09/14 18:55:16
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	new,placement new,no delete
//

#include <iostream>
#include <string>
#include <new>
using namespace std;
const int BUF = 512;
class JustTesting
{
private:
    string words;
    int number;

public:
    JustTesting(const string &s = "Just Teating", int n= 0)
    {
        words = s;
        number = n;
        cout << words << " constructed\n";
    }
    ~JustTesting() { cout << words << " destroyes\n"; }
    void Show() const { cout << words << ", " << number << endl; }
};
int main()
{
    char*buffer=new char[BUF];//buffer指向512个char型大小的内存区间

    JustTesting *pc1,*pc2;

    pc1=new(buffer) JustTesting;//使用定位new运算符再内存缓冲区创建类的对象
    pc2=new JustTesting("Heap1",20);

    cout<<"Memory block addresses:\n"<<"buffer:"
        <<(void *)buffer<<"     heap: "<<pc2<<endl;
    cout<<"Memory contents:\n";
    cout<<pc1<<": ";
    pc1->Show();
    cout<<pc2<<": ";
    pc2->Show();

    JustTesting *pc3,*pc4;
    pc3=new(buffer) JustTesting("Bad Idea",6);
    pc4=new JustTesting("Heap2",10);

     cout<<"Memory contents:\n";
    cout<<pc3<<": ";
    pc3->Show();
    cout<<pc4<<": ";
    pc4->Show();
    

    delete pc2;
    delete pc4;
    delete [] buffer;
    cout<<"Done\n";
    system("pause");
    return 0;
}